﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using GSM.BusinessLayer;
using GSM.BusinessLayer.DataLayer;

namespace CRM_SMS_MARKETING.PL.System_Management
{
    public partial class frmLogin : DevExpress.XtraEditors.XtraForm
    {
        List<GSM.BusinessLayer.UserAccount> ListAccount;
        UserAccountSql userAccountSql;

        public frmLogin()
        {
            InitializeComponent();
            ListAccount = new List<GSM.BusinessLayer.UserAccount>();
            userAccountSql = new UserAccountSql();
            txtPassword.Properties.UseSystemPasswordChar = true;

        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            ListAccount= userAccountSql.SelectByField("Deleted", 0);
            int Count = ListAccount.Count;
            bool checkEmpty = dxValidationProvider1.Validate();
            bool loginStatus=false;
            if (checkEmpty)
            {

                for (int i = 0; i < Count; i++)
                {
                    if ((ListAccount[i].UserName == txtUserName.Text && (ListAccount[i].Password == txtPassword.Text)))
                    {
                        loginStatus = true;
                        MessageBox.Show("Welcome " + txtUserName.Text + " Log in CRM SMS MARKETING", "Log in");
                        this.Hide();
                        frmMain frm = new frmMain(txtUserName.Text, ListAccount[i].UserType.ToString());
                        if (ListAccount[i].UserType.ToString() == "Admin")
                        {
                            frm.Admin();
                        }
                        else
                        {
                            frm.NormalUser();
                        }
                        frm.Show();
                        return;
                    }

                }
                if (loginStatus == false)
                {
                    MessageBox.Show("User Name or Password incorrect", "Log in error");
                }
            }
            else
            {
                MessageBox.Show("Bạn chưa nhập User Name hoặc Password!", "Lỗi đăng nhập", MessageBoxButtons.OK, MessageBoxIcon.Warning);            
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void linkLabelForgetPassword_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            frmForgotPassword frm = new frmForgotPassword();
            frm.Show();
            
        }
    }
}